require_relative 'lib'
require_relative 'slot'
require_relative 'macrofunc'
require_relative 'scheduler'

puts "Start"

=begin
# second example
$my_slots[0].left_apply(Attack)
$my_slots[0].left_apply(Zero)
$my_slots[0].right_apply(K)
$my_slots[0].right_apply(S)
$my_slots[0].left_apply(Succ)
$my_slots[0].left_apply(Zero)
$my_slots[0].right_apply(K)
$my_slots[0].right_apply(S)
$my_slots[0].left_apply(Get)
$my_slots[0].right_apply(K)
$my_slots[0].right_apply(S)
$my_slots[0].left_apply(Succ)
$my_slots[1].left_apply(Zero)
$my_slots[1].right_apply(Succ)
$my_slots[1].right_apply(Dbl)
$my_slots[1].right_apply(Dbl)
$my_slots[1].right_apply(Dbl)
$my_slots[1].right_apply(Dbl)
$my_slots[0].left_apply(Zero)
$my_slots[0].left_apply(Zero)
=end

=begin
# third example
$my_slots[0].left_apply(S)
$my_slots[0].left_apply(Get)
$my_slots[0].left_apply(I)
$my_slots[0].left_apply(Zero)

print_slots($my_slots, "my")
#print_slots($enemy_slots, "enemy")
=end

$prefered.reverse.take(30) . each { |e| puts e}
$my_slots[0].vitality = 0

def on_turn
  begin
    $scheduler.check_for_quick_revive

    if !is_there_commands?
      if($is_reviving_buffer)
        $scheduler.recover_from_revive
      end
      rand(3) != 0 ? $scheduler.choose_and_heal  : $scheduler.choose_and_kill
    end

    #p $command_queue
    $lr,$slot,$card = fetch_current_command
    #puts "#{$lr}, #{$slot}, #{$card}"
    do_application($lr, $slot, $card)
  rescue
    $lr = 1
    $card = 'I'
    $slot = 0
    do_application($lr, $slot, $card)
  end
end

step = 0
while $enemy_slots.find{|x| !x.dead?}

  #Scheduler.new.choose_and_kill
  #Scheduler.new.check_for_quick_revive
	#invoke_all
	#print_slots($my_slots, "my")
	#print_slots($enemy_slots, "enemy")
	#puts " "

  on_turn
  step+=1
  #if (step % 1000)==0
    print "step=#{step}"
    print_slots($my_slots, "my")
	  print_slots($enemy_slots, "enemy")
    gets
  #end

end